package com.game.element
{
	import com.game.manager.LayerManager;
	import com.game.view.scene.DiTuView;
	
	import flash.geom.Point;
	import flash.utils.getTimer;

	public class ZombieElement extends BaseElement
	{
		public var hp:Number = 200;
		public var maxHp:Number = 200;
		
		private var lastAtkTime:Number =0;
		private var fireInterval :Number = 1000;
		private var _damage:Number;
		public var getImpactForce:Number =0;
		
		public var baseAI:BaseAI;
		
		public function ZombieElement()
		{
			super();
		}
		
		public var speed:Number = 60;
		private var lastCheckTime:Number = 0;
		
		public function isCanAtk():Boolean{
			if( getTimer()-lastAtkTime <fireInterval ){
				return false;
			}
			lastAtkTime = getTimer();
			return true;
		}
		
		override public function exce():void {
			if(lastCheckTime ==0){
				lastCheckTime = getTimer();
				return;
			}
			var d:Number = getTimer()-lastCheckTime ;
			lastCheckTime = getTimer();
			
			var ds :Number = d/1000*speed;
			var p2:Point = getTarKey();
			if(p2 == null){
				return;
			}
			var p:Point = new Point(this.x,this.y);
			p2.normalize(ds);
			var dd:Point = p.add(p2);
			
			if(getImpactForce <0){//如果存在反力
				getImpactForce = 0
			}else if(getImpactForce >0){
				getImpactForce -=20;
				p2.normalize(ds);
				dd = p.subtract(p2);
			}
			
			var al:int = DiTuView.mask.getPixel32(dd.x/2,dd.y/2)>>6;
			if(al !=0){
				return;
			}
			
			this.x = dd.x;
			this.y = dd.y;
		}
		
		private function getTarKey():Point{
			var p1:Point = new Point(LayerManager.sceneView.playerView.x,LayerManager.sceneView.playerView.y);
			return p1.subtract(new Point(this.x,this.y));
		}
		
		public function get damage():Number{
			return Math.random()*20+10;
		}
		
		public function reduceHp(dam:Number,atk:BaseElement,impactForce:Number=0):Number{
			hp -= dam;
			getImpactForce = impactForce;
			if(hp <0){
				hp =0
			}
			return hp;
		}
	}
}